package com.example.dou2204.live;

import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.example.dou2204.entity.GiftEntity;

import java.util.Timer;
import java.util.TimerTask;

public class GiftControl {
    private GiftLayout giftLayout;
    int count;//发送礼物的数量
    long lastTime;//记录上次发送礼物的时间
    Timer timer;
    Handler handler = new Handler(){};

    public GiftControl(GiftLayout giftLayout) {
        this.giftLayout = giftLayout;
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(System.currentTimeMillis()-lastTime>3000){//距上次点击超过3秒，重新开始计数
                    count = 0;
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //更新UI  将暴击效果进行隐藏
                            giftLayout.setVisibility(View.GONE);
                        }
                    });
                }
            }
        },0,100);
    }


    public void loadGIft(GiftEntity.DataBean dataBean){
        if(count==0){//第一次发送礼物  是以连续点击为基础的        暴击效果显示  数量增加  保存点击的时间
            giftLayout.setVisibility(View.VISIBLE);
            count++;
            lastTime = System.currentTimeMillis();
        }else if(System.currentTimeMillis()-lastTime<=2000){//两秒内点击算作连续点击
            count++;
            lastTime = System.currentTimeMillis();
        }
        giftLayout.loadUser(SPUtils.getInstance().getString("icon"));
        giftLayout.loadUsername(SPUtils.getInstance().getString("username"));
        giftLayout.loadCount(count);
        giftLayout.loadGift(dataBean);
    }
}
